import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { PermissionPageParams } from '/@/api/shop-admin/permission/model/permissionModel';
import {getMenuList} from "/@/api/shop-admin/menu/menu";
import {computed, unref} from "vue";
import {RendererElement, RendererNode} from "@vue/runtime-core";
import {RenderCallbackParams} from "/@/components/Form";
import {values} from "lodash-es";
//import { menuDataSource } from '/@/views/shop-admin/menu/index';

export const menuColumns: BasicColumn[] = [
  {
    title: '菜单名称',
    dataIndex: 'name',
    key: 'name',
    width: 200,
    align: 'left',
  },
  /*{
    title: '图标',
    dataIndex: 'icon',
    width: 50,
    customRender: ({ record }) => {
      return h(Icon, { icon: record.icon });
    },
  },*/
  /*{
    title: '状态',
    dataIndex: 'status',
    width: 80,
    customRender: ({ record }) => {
      const status = record.status;
      const enable = ~~status === 0;
      const color = enable ? 'green' : 'red';
      const text = enable ? '启用' : '停用';
      return h(Tag, { color: color }, () => text);
    },
  },*/
  /*{
    title: '创建时间',
    dataIndex: 'createTime',
    width: 180,
  },*/
];

export const menuButtonColumns: BasicColumn[] = [
  {
    title: '权限名称',
    dataIndex: 'name',
    key: 'name',
    align: 'left',
  },
  {
    title: 'URL权限标识',
    dataIndex: 'urlPerm',
    key: 'urlPerm',
    // 设置成可伸缩列
    /*resizable: true,*/
    /*width: 100,
    minWidth: 100,
    maxWidth: 200,*/
    /*customRender: ({ record }) => {
      return h(Icon, { icon: record.icon });
    },*/
  },
  {
    title: '按钮权限标识',
    dataIndex: 'btnPerm',
    key: 'btnPerm',
  },
];

export const searchMenuFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '',
    component: 'Input',
    colProps: { span: 13 },
    //labelWidth: 120,
    componentProps: { placeholder: '菜单名称' },
  },
];

export const searchMenuButtonFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '',
    component: 'Input',
    colProps: { span: 13 },
    //labelWidth: 120,
    componentProps: { placeholder: '权限名称' },
  },
];

export const searchInfoParam: PermissionPageParams = {
  menuId: null,
  name: null,
};

const isMenu = (type: string) => type === '0';
const isButton = (type: string) => type === '1';
const isOutsideLink = (type: string) => type === '2';

const treeData = await getMenuList();
const menuOptions: any[] = [];
const menuOption = { parentId: '0', id: '0', name: '顶级菜单', children: treeData };
menuOptions.push(menuOption);
//treeData = menuOptions;

export const menuFormSchema: FormSchema[] = [
  {
    field: 'parentId',
    label: '父级菜单',
    component: 'TreeSelect',
    /*componentProps: {
      fieldNames: {
        label: 'name',
        key: 'id',
        value: 'id',
      },
      getPopupContainer: () => document.body,
    },*/
    componentProps: {
      treeData: menuOptions,
      fieldNames: {
        label: 'name',
        key: 'id',
        value: 'id',
      },
      getPopupContainer: () => document.body,
    },
    /*componentProps: ({ formModel }) => {
      return {
        treeData: menuDataSource,
        fieldNames: {
          label: 'name',
          key: 'id',
          value: 'id',
        },
        onChange: (value : any, label : any[]) => {
          formModel.parentName = label[0];
        },
        getPopupContainer: () => document.body,
      };
    },*/
  },
  {
    field: 'name',
    label: '菜单名称',
    component: 'Input',
    componentProps: {
      width: '20rem',
    },
    required: true,
  },
  {
    field: 'type',
    label: '菜单类型',
    component: 'RadioButtonGroup',
    defaultValue: '0',
    componentProps: {
      options: [
        { label: '菜单', value: '0' },
        { label: '目录', value: '1' },
        { label: '外链', value: '2' },
      ],
    },
    colProps: { lg: 24, md: 24 },
  },
  {
    field: 'routePath',
    //TODO 动态label更新
    label: isOutsideLink(values.type) ? '外链地址' : '路由路径',
    component: 'Input',
    required: true,
    /*ifShow: ({ values }) => !isOutsideLink(values.type),*/
  },
  {
    field: 'component',
    label: '组件路径',
    component: 'Input',
    ifShow: ({ values }) => isMenu(values.type),
  },
  {
    field: 'icon',
    label: '图标',
    component: 'IconPicker',
    required: true,
    /*ifShow: ({ values }) => !isOutsideLink(values.type),*/
  },
  {
    field: 'path',
    label: '跳转路由',
    component: 'Input',
    required: true,
  },
  {
    field: 'status',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: '0',
    componentProps: {
      options: [
        { label: '启用', value: '0' },
        { label: '禁用', value: '1' },
      ],
    },
  },
  {
    field: 'orderNo',
    label: '排序',
    component: 'InputNumber',
    required: true,
  },
];

export const permissionFormSchema: FormSchema[] = [
  {
    field: 'parentId',
    label: '父级菜单',
    component: 'TreeSelect',
    componentProps: {
      fieldNames: {
        label: 'name',
        key: 'id',
        value: 'id',
      },
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'name',
    label: '菜单名称',
    component: 'Input',
    required: true,
  },
  {
    field: 'type',
    label: '菜单类型',
    component: 'RadioButtonGroup',
    defaultValue: '0',
    componentProps: {
      options: [
        { label: '菜单', value: '0' },
        { label: '目录', value: '1' },
        { label: '外链', value: '2' },
      ],
    },
    colProps: { lg: 24, md: 24 },
  },
  {
    field: 'routePath',
    label: '路由地址',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'component',
    label: '组件路径',
    component: 'Input',
    ifShow: ({ values }) => isMenu(values.type),
  },
  {
    field: 'icon',
    label: '图标',
    component: 'IconPicker',
    required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'path',
    label: '跳转路由',
    component: 'Input',
    required: true,
  },
  {
    field: 'status',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: '0',
    componentProps: {
      options: [
        { label: '启用', value: '0' },
        { label: '禁用', value: '1' },
      ],
    },
  },
  {
    field: 'orderNo',
    label: '排序',
    component: 'InputNumber',
    required: true,
  },
];
